package com.dc.modules.sys.menu.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dc.commons.base.controller.DcBaseController;
import com.dc.commons.query.DcQuery;
import com.dc.commons.shiro.annotation.DcPermissions;
import com.dc.commons.shiro.enums.PermissionType;
import com.dc.commons.views.ModelAndViewBuilder;
import com.dc.modules.sys.menu.DcSysMenuService;
import com.dc.modules.sys.menu.domain.DcSysMenu;

/*******************************************************************************<P>
 * 文件名: DcSysMenuController.java <br>
 * 版本: 1.0<br>
 * 描述:<br>
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2016年6月23日 下午7:46:28<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 ******************************************************************************/
@Controller
@RequestMapping("/sys/menu")
public class DcSysMenuController extends DcBaseController {

	/**
	 * 菜单管理
	 * @param dcSysMenu
	 * @return
	 */
	@RequestMapping("/sysMenuManager")
	@DcPermissions(name="菜单管理",taskCode="sys:menu:sysMenuManager",permissionType=PermissionType.MENU)
	public ModelAndView sysMenuManager(DcSysMenu dcSysMenu){
		dcSysMenuService.sysMenuManager(dcSysMenu);
		
		return ModelAndViewBuilder.create()
				.success()
				.viewName("/sys/menu/sysMenuManager")
				.result(dcSysMenu)
				.build();
	}
	
	/**
	 * 菜单 查询
	 * @param dcSysMenu
	 * @return
	 */
	@RequestMapping("/querySysMenu")
	@DcPermissions(name="菜单管理",taskCode="sys:menu:sysMenuManager",repeat=true)
	public ModelAndView querySysMenu(DcQuery dcQuery){
		List<DcSysMenu> list=dcSysMenuService.queryList(dcQuery);
		
		return ModelAndViewBuilder.create()
				.success()
				.result(list)
				.build();
	}
	
	/**
	 * 到菜单新增页面
	 * @param dcSysMenu
	 * @return
	 */
	@RequestMapping("/sysMenuAdd")
	@DcPermissions(name="新增菜单",taskCode="sys:menu:sysMenuAdd",permissionType=PermissionType.TASK)
	public ModelAndView sysMenuAdd(DcSysMenu dcSysMenu){
		
		return ModelAndViewBuilder.create()
				.success()
				.viewName("/sys/menu/sysMenuAdd")
				.result(dcSysMenu)
				.build();
	}
	
	/**
	 * 新增菜单
	 * @param dcSysMenu
	 * @return
	 */
	@RequestMapping("/addSysMenu")
	@DcPermissions(name="新增菜单",taskCode="sys:menu:sysMenuAdd",repeat=true)
	public ModelAndView addSysMenu(DcSysMenu dcSysMenu){
		dcSysMenuService.addSysMenu(dcSysMenu);
		
		return ModelAndViewBuilder.create()
				.success()
				.result(dcSysMenu)
				.build();
	}
	
	/**
	 * 到菜单修改页面
	 * @param dcSysMenu
	 * @return
	 */
	@RequestMapping("/sysMenuEdit")
	@DcPermissions(name="修改菜单",taskCode="sys:menu:sysMenuEdit",permissionType=PermissionType.TASK)
	public ModelAndView sysMenuEdit(DcSysMenu dcSysMenu){
		
		return ModelAndViewBuilder.create()
				.success()
				.viewName("/sys/menu/sysMenuEdit")
				.result(dcSysMenu)
				.build();
	}
	
	/**
	 * 修改菜单
	 * @param dcSysMenu
	 * @return
	 */
	@RequestMapping("/editSysMenu")
	@DcPermissions(name="修改菜单",taskCode="sys:menu:sysMenuEdit",repeat=true)
	public ModelAndView editSysMenu(DcSysMenu dcSysMenu){
		dcSysMenuService.editSysMenu(dcSysMenu);
		
		return ModelAndViewBuilder.create()
				.success()
				.result(dcSysMenu)
				.build();
	}
	
	/**
	 * 菜单详情
	 * @param dcSysMenu
	 * @return
	 */
	@RequestMapping("/sysMenuDetail")
	@DcPermissions(name="菜单详情",taskCode="sys:menu:sysMenuDetail",permissionType=PermissionType.TASK)
	public ModelAndView sysMenuDetail(DcSysMenu dcSysMenu){
		
		return ModelAndViewBuilder.create()
				.success()
				.viewName("/sys/menu/sysMenuDetail")
				.result(dcSysMenu)
				.build();
	}
	
	
	/**
	 * 删除菜单
	 * @param dcSysMenu
	 * @return
	 */
	@RequestMapping("/deleteSysMenu")
	@DcPermissions(name="删除菜单",taskCode="sys:menu:sysMenuDelete",permissionType=PermissionType.TASK)
	public ModelAndView deleteSysMenu(DcSysMenu dcSysMenu){
		dcSysMenuService.deleteSysMenu(dcSysMenu);
		
		return ModelAndViewBuilder.create()
				.success()
				.result(dcSysMenu)
				.build();
	}
	
	/**
	 * 拖动菜单
	 * @param dcQuery
	 * @return
	 */
	@RequestMapping("/dragSysMenu")
	@DcPermissions(name="拖动菜单",taskCode="sys:menu:sysMenuDrag",permissionType=PermissionType.TASK)
	public ModelAndView dragSysMenu(DcQuery dcQuery){
		dcSysMenuService.dragSysMenu(dcQuery);
		
		return ModelAndViewBuilder.create()
				.success()
				.build();
	}
	
	/**
	 *  如果参数中存在 id 则根据id查询到对象。
	 * @param id
	 * @param model
	 */
	@ModelAttribute
	public void get(@RequestParam(required=false,name="id") String id,Model model) {
		DcSysMenu sysMenu=new DcSysMenu();
		if (StringUtils.isNotEmpty(id)){
			sysMenu=dcSysMenuService.findSysMenu(id);
			model.addAttribute("dcSysMenu", sysMenu);
		}
	}
	
	@Autowired
	private DcSysMenuService dcSysMenuService;
	
}
